package gd.hq.yolov5.camera;

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import gd.hq.yolov5.MainActivity
import gd.hq.yolov5.R
import gd.hq.yolov5.comunicate.BluetoothActivity
import gd.hq.yolov5.yolo.Yolo
import kotlinx.android.synthetic.main.activity_camera.*
import java.util.*
import kotlin.concurrent.thread


/**
 * author :  chensen
 * data  :  2018/3/18
 * desc :
 */
class CameraActivity : AppCompatActivity() {
    companion object {
        const val TYPE_TAG = "type"
        const val TYPE_CAPTURE = 0
        const val TYPE_RECORD = 1
    }


    private lateinit var mCameraHelper: CameraHelper
    private lateinit var nV21ToBitmap: NV21ToBitmap
    private lateinit var yolo: Yolo
    private lateinit var yoloHandler:Handler
    var imgQueue: Queue<Bitmap> = ArrayDeque()
    var resultQueue: Queue<Bitmap> = ArrayDeque()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_camera)
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        yolo = Yolo(this)
        yoloHandler =  Handler()
        nV21ToBitmap = NV21ToBitmap(this)
        mCameraHelper = CameraHelper(this, surfaceView)
        mCameraHelper.addCallBack(object : CameraHelper.CallBack {
            override fun onTakePic(data: ByteArray?) {
                btnTakePic.isClickable = true
            }

            override fun onPreviewFrame(data: ByteArray?,width: Int,height:Int) {
                Log.d("data","frame  "+width.toString()+height.toString());

                var frame = nV21ToBitmap.nv21ToBitmap(data,width,height)
                imgQueue.add(NV21ToBitmap.resizeBitmap(frame, (width*0.4).toInt(), (height*0.4).toInt()))
//                Log.d("thread",Thread.currentThread().getName())
//                var result = yolo.detect_with_box(frame,0.3f,0.7f)
//                btnTakePic.setImageBitmap(frame)
//                this@CameraActivity.runOnUiThread {   }
//                btnTakePic.setImageBitmap(frame)
            }
        })

        ivExchange.setOnClickListener { mCameraHelper.exchangeCamera() }
        ivSetting.setOnClickListener { _ ->
            val intent = Intent(CameraActivity@ this, MainActivity::class.java)
            startActivity(intent);
        }

        configBle.setOnClickListener { _ ->
            val intent = Intent(CameraActivity@ this, BluetoothActivity::class.java)
            startActivity(intent);
        }

        thread {
            while (true) {
                if (!imgQueue.isEmpty()) {
                    var frame = imgQueue.remove()
                    var result = yolo.detect_with_box(frame, 0.3f, 0.7f)
                    this.runOnUiThread { btnTakePic.setImageBitmap(result) }
                    imgQueue.clear()
                }
            }
        }


    }

//    private fun savePic(data: ByteArray?) {
//        thread {
//            try {
//
//            } catch (e: Exception) {
//                e.printStackTrace()
//                runOnUiThread {
////                    toast("保存图片失败！")
//                }
//            }
//        }
//    }

    override fun onDestroy() {
        mCameraHelper.releaseCamera()
        super.onDestroy()
    }

}